package com.miplus.ccrm.workflow.nodes.impl;

import com.miplus.ccrm.workflow.model.enums.VersionType;
import com.miplus.ccrm.workflow.logic.LogicFlow;
import com.miplus.ccrm.workflow.nodes.BaseFlowConverter;
import com.miplus.ccrm.workflow.nodes.IFlowConverter;

public class V2FlowConverter extends BaseFlowConverter implements IFlowConverter {
    @Override
    public String converter(LogicFlow _logicFlow) {
        return "";
    }

    @Override
    public VersionType getVersionType() {
        return VersionType.CMV2;
    }

    @Override
    public String print(String param) {
        String[] template = new String[]{
                tryCatch(new String[]{
                        "Log.Debug("+param+");",
                        ""
                },new String[]{"Log.Error(e.message);"},"e")
        };
        // 判断param 是否为数字，如果不是数字则加上引号
        return String.format("Log.Debug(%s);", param);
    }

    @Override
    public String tryCatch(String tryBlock, String catchBlock, String exceptionVar) {
        if(exceptionVar == null || exceptionVar.isEmpty()){
            exceptionVar = "e";
        }
        return String.format("try {\n%s\n} catch (%s) {\n%s\n}", tryBlock, exceptionVar, catchBlock);
    }


    @Override
    public String declareVar(String varName, String varValue,String functionName) {
        return String.format("globalValue.%s.%s=%s;", functionName.isEmpty() ? functionName : "all",varName, varValue);
    }

    @Override
    public String globalVar(String varName, String varValue) {
        return "globalValue." + varName + " = " + varValue + ";";
    }

    @Override
    public String Template() {
        String[] template = new String[]{
                "let globalValue = {};",
                "globalValue = {...session,all:{}}",
                "(function{",
                "",
                "})();",
        };
        return String.join("\n", template);
    }

    @Override
    public String getDeclareVar(String pathVarName) {

        return "";
    }

    @Override
    public String getNowDateTime() {
        return "new Date().toISOString()";
    }


}
